<?
/*
############## KATEGORIJOS ##############
CREATE TABLE `f_kategorijos` (
  `id` int(11) NOT NULL auto_increment,
  `pavadinimas` varchar(100) NOT NULL,
  `nr` int(2) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

############# TEMOS #####################
CREATE TABLE `f_temos` (
  `id` int(10) NOT NULL auto_increment,
  `kid` int(10) NOT NULL,
  `pavadinimas` varchar(110) NOT NULL,
  `apie` text,
  `nr` int(10) NOT 0,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
INSERT INTO `f_temos` (`id`, `kid`, `pavadinimas`, `apie`, `nr`) VALUES (NULL, '3', 'Komandos', 'Diskusijos apie mIRC komandas', '1');
############ FORUMUI ####################
CREATE TABLE `f_forumai` (
  `id` int(10) NOT NULL auto_increment,
  `kid` int(10) NOT NULL,
  `tid` int(10) NOT NULL,
  `pav` varchar(110) NOT NULL,
  `autorius` int(10) NOT NULL,
  `data` datetime NOT NULL,
  `perziuru` int(10) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

############ ZINUTES ####################
CREATE TABLE `f_zinutes` (
  `id` int(10) NOT NULL auto_increment,
  `tid` int(10) NOT NULL,
  `kid` int(10) NOT NULL,
  `fid` int(10) NOT NULL,
  `autorius` int(1) NOT NULL,
  `data` datetime NOT NULL,
  `ip` varchar(11) NOT NULL,
  `failas` varchar(110) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

################# STRUKTURA #########################
# Kategorija	>	Temos	> Forumai	> 	Zinutes
# id			>	kid		>	tid	 	>	fid
*/

if (isset($url['k']) && isnum($url['k']) && $url['k'] > 0) { $kid = (int)$url['k']; } else { $kid = 0; } // Kategorijos ID
if (isset($url['t']) && isnum($url['t']) && $url['t'] > 0) { $tid = (int)$url['t']; } else { $tid = 0; } // Temos ID
if (isset($url['f']) && isnum($url['f']) && $url['f'] > 0) { $fid = (int)$url['f']; } else { $fid = 0; } // Forumo ID
if (isset($url['p']) && isnum($url['p']) && $url['p'] > 0) { $pid = (int)$url['p']; } else { $pid = 0; } // Puslapiavimo ID
if (isset($url['d']) && isnum($url['d']) && $url['d'] > 0) { $did = (int)$url['d']; } else { $did = 0; } // Trinamos zinutes ID
if (isset($url['l']) && isnum($url['l']) && $url['l'] > 0) { $lid = (int)$url['l']; } else { $lid = 0; } // Rakinamos temos ID

# Skaitom kategorijas ir temas
# Darau su funkcijom tam kad kintamuju nereiktu valyt :)
if ($kid == 0) {
	lentele("Navigacija","<strong>Forumas</strong> >");
	kategorijos();
}
# Rodom temas
elseif ($tid > 0 && $fid == 0) {
	temos($kid,$tid);
	//klaida("Klaida","Šios dalies dar nebaigiau");
}
# Ziurim postus
elseif ($fid > 0) {
	# IP isgauti "INET_NTOA(ip) AS ip"
	# IP ideti "`ip` = INET_ATON(".escape($test['ip']).")"
	lentele("Navigacija","<a href='?id,66'><strong><span style='color:red;'>««</span> Grįžti į Forumą</strong></a>");
	zinutes($kid,$tid,$fid);
	//klaida("Klaida","Šios dalies dar net nepradėjau :)");
}

# kategoriju atvaizdavimui (pirmas puslapis)
function kategorijos() {
	$sql = mysql_query1("SELECT `id`, `pavadinimas` FROM `f_kategorijos` ORDER BY `nr`");
	if (mysql_affected_rows() == 0) { lentele("Forumas",'Nėra sukurtų kategorijų'); } else { 
		while ($row = mysql_fetch_assoc($sql)) {
			$sql1 = mysql_query1("SELECT `f_temos`.*,
		(SELECT Count(`f_forumai`.`id`) FROM `f_forumai` WHERE `f_forumai`.`tid` = `f_temos`.`id` AND `f_forumai`.`kid` = ".escape($row['id']).") AS `temu`,
		(SELECT Count(`f_zinutes`.`id`) FROM `f_zinutes` WHERE `f_zinutes`.`tid` = `f_temos`.`id` AND `f_zinutes`.`kid` = ".escape($row['id']).") AS `zinuciu`
		FROM `f_temos` WHERE `f_temos`.`kid`=".escape($row['id'])." ORDER BY `f_temos`.`nr`") or die(mysql_error());
	$text = '
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="0" bordercolor="#000000">
  <tr align="center">
	<td colspan="2" nowrap="nowrap" align="left" style="padding-left:18px">Pavadinimas</td>
	<td width="5%" nowrap="nowrap">Temų</td>
	<td width="6%" nowrap="nowrap">Žinučių</td>
	<td width="23%" nowrap="nowrap">Paskutinis pranešimas </td>
  </tr>';
			while ($row1 = mysql_fetch_assoc($sql1)) {
				$info = mysql_fetch_assoc(mysql_query1("SELECT `users`.`nick`, `users`.`id` AS `nick_id`, `f_zinutes`.`tid`, `f_zinutes`.`kid`, `f_zinutes`.`fid`, `f_zinutes`.`data` FROM `f_zinutes` Inner Join `users` ON `f_zinutes`.`autorius` = `users`.`id` WHERE `f_zinutes`.`tid` = ".escape($row1['id'])." AND `f_zinutes`.`kid` = ".escape($row['id'])." ORDER BY `f_zinutes`.`data` DESC LIMIT 1"));
				$text .= '
  <tr align="center">
	<td width="1%"><strong><font color="silver">&#8226;</font></strong></td>
	<td width="65%" align="left"><strong><a href="'.url("k,".$row['id'].";t,".$row1['id']."").'">'.$row1['pavadinimas'].'</a></strong><br/><span class="comment">'.$row1['apie'].'</span></td>
	<td width="5%">'.$row1['temu'].'</td>
	<td width="6%">'.$row1['zinuciu'].'</td>
	<td width="23%"><a href="?id,47;m,'.$info['nick_id'].'">'.$info['nick'].'</a><br/>'.kada($info['data']).'</td>
  </tr>';
			}
			$text .= "
</table>";
			lentele("<a href=\"#".$row['id']."\" name=\"".$row['id']."\" id=\"".$row['id']."\"><img src=\"images/icons/anchor.png\" alt=\"#\" class=\"middle\" border=\"0\"></a> ".$row['pavadinimas'],$text);
		}
	}
}

# Forumu atvaizdavimui
function temos($kid,$tid) {
	$sql = mysql_query1("SELECT `users`.`nick`, `f_forumai`.`id`, `f_forumai`.`pav`, `f_forumai`.`perziuru`, `f_forumai`.`autorius` AS `nick_id`, `f_temos`.`pavadinimas`,
(SELECT Count(`f_zinutes`.`id`) FROM `f_zinutes` WHERE `f_zinutes`.`tid` = ".escape($tid)." AND `f_zinutes`.`kid` = ".escape($kid).") AS `zinuciu`
FROM `users` Inner Join `f_forumai` ON `f_forumai`.`autorius` = `users`.`id` , `f_temos`
WHERE `f_forumai`.`tid` = `f_temos`.`id` AND `f_temos`.`kid` = ".escape($kid)."");
	if (mysql_affected_rows() == 0) { lentele("Forumas",'Nėra sukurtų temų'); } else { 
$text = '<table width="100%" border="0" align="center" cellpadding="3" cellspacing="0" bordercolor="#000000">
  <tr style="background-color : #cccccc;" align="center">
	<td colspan="2" nowrap="nowrap" align="left" style="padding-left:20px">Pavadinimas</td>
	<td width="5%" nowrap="nowrap">Autorius</td>
	<td width="6%" nowrap="nowrap">Žinučių</td>
	<td width="23%" nowrap="nowrap">Paskutinis pranešimas </td>
  </tr>';
		while ($row = mysql_fetch_assoc($sql)) {
 $text .= '
  <tr align="center" class="sarasas">
	<td width="1%"><strong><font color="silver">&#8226;</font></strong></td>
	<td width="65%" align="left"><strong><a href="'.url("k,".$kid.";t,".$tid.";f,".$row['id']."").'">'.$row['pav'].'</a></strong></td>
	<td width="5%"><a href="?id,47;m,'.$row['nick_id'].'">'.$row['nick'].'</a></td>
	<td width="6%">'.$row['zinuciu'].'</td>
	<td width="23%"></td>
  </tr>'; $pav = $row['pavadinimas'];
	  		}
$text .= "
</table>";
		lentele("Navigacija","<a href='?id,66'><strong>Forumas</strong></a> > ".$pav."");
		lentele("Įrankiai","<img src='images/forum/post.gif' alt='naujas' border='0'/>");
		lentele("Temų sarašas",$text);
		lentele("Įrankiai","<img src='images/forum/post.gif' alt='naujas' border='0'/>");
	}
}

function zinutes($kid=0,$tid=0,$fid=0) {
	if ($kid==0 || $tid==0 || $fid==0) { klaida("Klaida","Uptiktas pažeidimas.<h3>Jums priskaičiuotas 1 baudos balas</h3>Surinkus 5 baudos balus - <b>būsite užbanintas</b>"); redirect("?id,".$url['id'],"meta"); exit; }
	else {
		$sql = mysql_query("
		SELECT
		`users`.`nick`,
		`users`.`avatar`,
		`users`.`levelis`,
		`users`.`icq`,
		`users`.`msn`,
		`users`.`skype`,
		`users`.`yahoo`,
		`users`.`aim`,
		`users`.`url`,
		`users`.`salis`,
		`users`.`miestas`,
		`users`.`vardas`,
		`users`.`gim_data`,
		`users`.`parasas`,
		`users`.`taskai`,
		`users`.`login_data`,
		`users`.`balsai`,
		`f_zinutes`.`id`,
		`f_zinutes`.`zinute`,
		`f_zinutes`.`sypsenos`,
		`f_zinutes`.`autorius` AS `nick_id`,
		`f_zinutes`.`data`,
		(SELECT Count(`f_zinutes`.`id`) FROM `f_zinutes` WHERE `f_zinutes`.`autorius` = `nick_id`) AS `zinuciu`,
		INET_NTOA(`f_zinutes`.`ip`) AS `ip`,
		`f_zinutes`.`failas`
		FROM
		`f_zinutes`
		Left Join `users` ON `f_zinutes`.`autorius` = `users`.`id`
		WHERE
		`f_zinutes`.`kid` =  ".escape($kid)." AND
		`f_zinutes`.`tid` =  ".escape($tid)." AND
		`f_zinutes`.`fid` =  ".escape($fid)."
		ORDER BY
		`f_zinutes`.`data` ASC
		LIMIT 1, 30") or die(mysql_error());
		if (mysql_affected_rows() == 0) { klaida("Klaida",'Nėra pranešimų'); } else { 
			while ($row = mysql_fetch_assoc($sql)) {
				$title = '<span class="style1">•</span> <a href="?id,47;m,'.$row['nick_id'].'" title="header=['.$row['nick'].'] body=[<br/>
				<p><img src=\'images/levelis/'.$row['levelis'].'.gif\' alt=\''.$row['nick'].'\'/></p>
						Amžius: <strong>'.amzius($row['gim_data']) .'</strong><br/>
						Taškai: <strong>'.$row['taskai'].'</strong><br/>
						Miestas: <strong>'.$row['miestas'].'</strong><br/>
						Žinutės: <strong>'.$row['zinuciu'].'</strong><br/>
						Lankėsi: <strong>'.kada($row['login_data']).'</strong><br/>
				
				<br/>] cssbody=[sarasas] cssheader=[title] fade=[on]">'.$row['nick'].'</a> <strong><a href="javascript:window.scroll(0,0)"><font size="1">↑↑</font></a></strong>';
				$zinute = bbcode(($row['sypsenos']==1)?smyle($row['zinute']):$row['zinute']);
$text = '
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" style="margin:10px">
  <tr>
	<td width="100" rowspan="2" align="center" valign="top">
	<p><img src="'.$row['avatar'].'" width="120" alt="kauke"/></p>
	</td>
	<td height="100" colspan="2" valign="top" style="padding:5px">'.$zinute.'</td>
  </tr>
  
  <tr>
	<td height="5" valign="middle">Irankiai</td>
  </tr>
</table>
';
				lentele($title,$text);
			}
		}
	}
}
?>